package com.yxinsur.imc.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.*;

import com.yxinsur.imc.business.domain.ImcRateFactorValue;
import com.yxinsur.imc.business.enums.IsDeletedEnum;
import com.yxinsur.imc.business.form.RateFactorForm;
import com.yxinsur.imc.business.mapper.ImcRateFactorValueMapper;
import com.yxinsur.imc.business.vo.InsFactorVo;
import com.yxinsur.imc.business.vo.RateFactorKeyVo;
import com.yxinsur.imc.common.utils.DateUtils;
import com.yxinsur.imc.common.utils.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yxinsur.imc.business.mapper.ImcRateFactorKeyMapper;
import com.yxinsur.imc.business.domain.ImcRateFactorKey;
import com.yxinsur.imc.business.service.IImcRateFactorKeyService;

/**
 * 产品费率因子KEYService业务层处理
 * 
 * @author imc
 * @date 2021-03-30
 */
@Service
public class ImcRateFactorKeyServiceImpl extends ServiceImpl<ImcRateFactorKeyMapper, ImcRateFactorKey> implements IImcRateFactorKeyService
{
    private static final Logger logger = LoggerFactory.getLogger(ImcRateFactorKeyServiceImpl.class);

    @Autowired
    private ImcRateFactorKeyMapper imcRateFactorKeyMapper;
    @Autowired
    private ImcRateFactorValueMapper imcRateFactorValueMapper;

    /**
     * 查询产品费率因子KEY列表
     * 
     * @param rateFactorKeyForm 产品费率因子KEY
     * @return 产品费率因子KEY
     */
    @Override
    public List<RateFactorKeyVo> selectImcRateFactorKeyList(RateFactorForm rateFactorKeyForm)
    {
        logger.info("查询产品费率因子KEY列表，条件：{}",rateFactorKeyForm.toString());
        return imcRateFactorKeyMapper.selectImcRateFactorKeyList(rateFactorKeyForm);
    }

    /**
     * 查询产品所有因子列表
     *
     * @param repoInsId 产品ID
     * @return 所有因子列表
     */
    @Override
    public List<InsFactorVo> getInsFactorListByRepoInsId(Long repoInsId) {
        logger.info("根据产品ID查询产品费率因子列表，条件：{}",repoInsId);
        QueryWrapper<ImcRateFactorKey> keyWrapper = new QueryWrapper<>();
        keyWrapper.eq("repo_ins_id",repoInsId);
        keyWrapper.eq("is_deleted",IsDeletedEnum.N.getCode());
        keyWrapper.eq("tenant_id",SecurityUtils.getTenantId());
        keyWrapper.orderByAsc("id");
        List<ImcRateFactorKey> keys = this.imcRateFactorKeyMapper.selectList(keyWrapper);
        List<InsFactorVo> insFactorVos = new ArrayList<>();
        for (ImcRateFactorKey key:keys) {
            InsFactorVo vo = new InsFactorVo();
            vo.setFactorKeyId(key.getId());
            vo.setFactorKey(key.getFactorKey());
            QueryWrapper<ImcRateFactorValue> valueWrapper = new QueryWrapper<>();
            valueWrapper.eq("rate_factor_key_id",key.getId());
            valueWrapper.eq("is_deleted",IsDeletedEnum.N.getCode());
            valueWrapper.eq("tenant_id",SecurityUtils.getTenantId());
            valueWrapper.orderByAsc("id");
            List<ImcRateFactorValue> values = this.imcRateFactorValueMapper.selectList(valueWrapper);
            List<Map<String,Object>> valueMaps = new ArrayList<>();
            for (ImcRateFactorValue value:values) {
                Map<String,Object> map = new HashMap<>();
                map.put("factorValue",value.getFactorValue());
                map.put("factorValueId",value.getId());
                valueMaps.add(map);
            }
            vo.setFactorValues(valueMaps);
            insFactorVos.add(vo);
        }
        return insFactorVos;
    }

    /**
     * 新增产品费率因子
     * 
     * @param reporInsId 险种id
     * @param rateFactorForms 产品费率因子
     * @return 结果
     */
    @Override
    public int insertImcRateFactor(Long reporInsId,List<RateFactorForm> rateFactorForms)
    {
        logger.info("新增产品费率因子操作：{},{}",reporInsId,rateFactorForms);
        Date date = DateUtils.getNowDate();
        String username = SecurityUtils.getUsername();
        for (RateFactorForm form:rateFactorForms) {
            ImcRateFactorKey rateFactorKey = new ImcRateFactorKey();
            rateFactorKey.setFactorKey(form.getFactorKey());
            rateFactorKey.setRepoInsId(reporInsId);
            rateFactorKey.setCreateTime(date);
            rateFactorKey.setCreateId(username);
            rateFactorKey.setIsDeleted(IsDeletedEnum.N.getCode());
            rateFactorKey.setTenantId(SecurityUtils.getTenantId());
            this.imcRateFactorKeyMapper.insert(rateFactorKey);

            String values[] = form.getFactorValues().split(",");
            Long rateFactorKeyId = Long.valueOf(rateFactorKey.getId());
            for (String value:values) {
                ImcRateFactorValue rateFactorValue = new ImcRateFactorValue();
                rateFactorValue.setFactorValue(value);
                rateFactorValue.setRateFactorKeyId(rateFactorKeyId);
                rateFactorValue.setCreateTime(date);
                rateFactorValue.setCreateId(username);
                rateFactorValue.setIsDeleted(IsDeletedEnum.N.getCode());
                rateFactorValue.setTenantId(SecurityUtils.getTenantId());
                this.imcRateFactorValueMapper.insert(rateFactorValue);
            }
        }
        return rateFactorForms.size();
    }

    /**
     * 批量删除产品费率因子KEY
     * 
     * @param repoInsIds 需要删除的产品id
     * @return 结果
     */
    @Override
    public int deleteImcRateFactorKeyByRepoInsIds(Long[] repoInsIds)
    {
        logger.info("根据repoInsId批量删除产品费率因子操作：{}",repoInsIds);
        int count = 0;
        Date date = DateUtils.getNowDate();
        String username = SecurityUtils.getUsername();
        for(Long repoInsId:repoInsIds){
            QueryWrapper<ImcRateFactorKey> keyQueryWrapper = new QueryWrapper<>();
            keyQueryWrapper.eq("repo_ins_id",repoInsId);
            keyQueryWrapper.eq("is_deleted",IsDeletedEnum.N.getCode());
            keyQueryWrapper.eq("tenant_id",SecurityUtils.getTenantId());
            List<ImcRateFactorKey> keys = this.imcRateFactorKeyMapper.selectList(keyQueryWrapper);
            for(ImcRateFactorKey key:keys){
                key.setIsDeleted(key.getId().toString());
                key.setUpdateId(username);
                key.setUpdateTime(date);
                count += this.imcRateFactorKeyMapper.updateById(key);
                QueryWrapper<ImcRateFactorValue> valueUpdateWrapper = new QueryWrapper<>();
                valueUpdateWrapper.eq("rate_factor_key_id",key.getId());
                valueUpdateWrapper.eq("is_deleted",IsDeletedEnum.N.getCode());
                List<ImcRateFactorValue> values = this.imcRateFactorValueMapper.selectList(valueUpdateWrapper);
                for(ImcRateFactorValue value:values){
                    value.setIsDeleted(value.getId());
                    value.setUpdateId(username);
                    value.setUpdateTime(date);
                    this.imcRateFactorValueMapper.updateById(value);
                }
            }
        }
        return count;
    }

}
